package com.hang.ixin.service.impl;

import com.hang.ixin.Enums.AddFriendStatusEnum;
import com.hang.ixin.Enums.MsgSignEnum;
import com.hang.ixin.POJO.ChatMsg;
import com.hang.ixin.POJO.DTO.MsgDTO;
import com.hang.ixin.POJO.FriendsRequest;
import com.hang.ixin.POJO.MyFriends;
import com.hang.ixin.POJO.Users;
import com.hang.ixin.dao.ChatMsgDao;
import com.hang.ixin.dao.FriendsRequestDao;
import com.hang.ixin.dao.MyfriendsDao;
import com.hang.ixin.dao.UsersDao;
import com.hang.ixin.service.UserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersDao usersDao;
    @Autowired
    private MyfriendsDao myfriendsDao;
    @Autowired
    private FriendsRequestDao friendsRequestDao;
    @Autowired
    private Sid sid;
    @Autowired
    private ChatMsgDao chatMsgDao;

    //登录
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users login(String username, String password) {
        return usersDao.findUsersByUsernameAndPassword(username,password);
    }

    //更新头像
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateImage(Users users) {
        return usersDao.save(users);
    }

    //通过id查找users
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users findOne(String id) {
        return usersDao.findById(id).get();
    }

    //更新用户昵称
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateNickname(Users users) {
        return usersDao.save(users);
    }

    //通过username查找user
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users findUsersByUsername(String username) {
        return usersDao.findUsersByUsername(username);
    }

    //找人的前置条件判断
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Integer findInConditions(String myId, String friendUsername) {

        Users users = usersDao.findUsersByUsername(friendUsername);
        if (users == null){
            return AddFriendStatusEnum.USER_NOT_EXSIST.getCode();
        }

        if(users.getId().equals(myId)){
            return AddFriendStatusEnum.URSELF.getCode();
        }

        if(searchFriend(myId,users.getId()) != null){
            return AddFriendStatusEnum.ALREADY_ADDED.getCode();
        }


        return AddFriendStatusEnum.SUCCESS.getCode();
    }

    //通过username找人
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public MyFriends searchFriend(String myId, String friendId) {
        return myfriendsDao.findByMyUserIdAndMyFriendUserId(myId,friendId);
    }

    //判断添加好友请求表中有无请求数据
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void requestExist(String myid, String friendId) {
        FriendsRequest friendsRequest = friendsRequestDao.findBySendUserIdAndAcceptUserId(myid,friendId);

        if (friendsRequest == null){
            FriendsRequest request = new FriendsRequest();
            request.setId(sid.nextShort());
            request.setSendUserId(myid);
            request.setAcceptUserId(friendId);
            request.setRequestDateTime(new Date());
            friendsRequestDao.save(request);
        }
    }

    //添加好友
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<FriendsRequest> friendsRequest(String acceptUserId) {
        return friendsRequestDao.findByAcceptUserId(acceptUserId);
    }

    //查找Users列表
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public List<Users> findAllByIdIn(List<String> list) {
        return usersDao.findAllByIdIn(list);
    }

    //忽略请求，删除表数据
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteReq(String sendUserId, String acceptUserId) {
        friendsRequestDao.deleteBySendUserIdAndAcceptUserId(sendUserId,acceptUserId);
    }

    //通过请求，互加好友，删除表数据
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void passReq(String sendUserId, String acceptUserId) {
        MyFriends myFriends = new MyFriends();
        MyFriends myFriends1 = new MyFriends();
        myFriends.setId(sid.nextShort());
        myFriends.setMyUserId(sendUserId);
        myFriends.setMyFriendUserId(acceptUserId);
        myFriends1.setId(sid.nextShort());
        myFriends1.setMyUserId(acceptUserId);
        myFriends1.setMyFriendUserId(sendUserId);
        List<MyFriends> list = new ArrayList<>();
        myfriendsDao.save(myFriends);
        myfriendsDao.save(myFriends1);

        friendsRequestDao.deleteBySendUserIdAndAcceptUserId(sendUserId,acceptUserId);

    }

    //通过我的ID查找我的朋友列表
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<MyFriends> findFriends(String myId) {
        return myfriendsDao.findByMyUserId(myId);
    }

    //储存聊天消息
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public String saveMsg(MsgDTO msgDTO) {
        ChatMsg chatMsg = new ChatMsg();
        BeanUtils.copyProperties(msgDTO,chatMsg);
        String msgId = sid.nextShort();
        chatMsg.setId(msgId);
        chatMsg.setSignFlag(MsgSignEnum.UNSIGNED.getCode());
        chatMsg.setCreateTime(new Date());
        chatMsgDao.save(chatMsg);
        return msgId;
    }

    //通过id列表批量查找ChatMsg
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ChatMsg> findChatMsgListByIds(List<String> list) {
        return chatMsgDao.findAllByIdIn(list);
    }

    //批量储存ChatMsg
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateChatMsgList(List<ChatMsg> list) {
        chatMsgDao.saveAll(list);
    }

    //签收消息
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ChatMsg> getUnsignMsg(String accepterId, Integer signFlag) {
        return chatMsgDao.findAllByAcceptUserIdAndSignFlag(accepterId, signFlag);
    }


}
